package com.fei.BYSJ.controller;

import com.fei.BYSJ.beans.Data;
import com.fei.BYSJ.entity.User;
import com.fei.BYSJ.service.UserService;
import com.fei.BYSJ.utils.HttpServletRequestUtil;
import com.fei.BYSJ.utils.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends ResultData{
    @Autowired
    UserService userService;

    /*@RequestMapping("/userlist")
    public @ResponseBody
    List<User> getUserList(){
       *//* PageHelper.startPage(pageNumber, pageSize);
        List<UserInfo> lists = userInfoMapper.findAll();

        PageInfo<UserInfo> pageInfo = new PageInfo<>(lists);

        return success("success", (int) pageInfo.getTotal(),pageInfo.getList());*//*

        return userService.findAll();
    }*/

    @RequestMapping("/personalCenter")
    public ModelAndView personalCenter(HttpSession session, HttpServletRequest request){
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("user/personalCenter");
        modelAndView.addObject("orderlist",userService.findOne(user.getUserid()));

        return modelAndView;
    }

    @RequestMapping("/updateUserMessage")
    public ModelAndView updateUserMessage(HttpSession session, HttpServletRequest request){
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("user/personalCenter");
        modelAndView.addObject("u",userService.findOne(user.getUserid()));

        return modelAndView;    }





    @RequestMapping("/updateUserInformation")
    public @ResponseBody
    Data updateUserInformation(HttpSession session, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("user/personalCenter");
        User user = (User) session.getAttribute("user");
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        System.out.println(user.getUseraccount().equals(params.get("userAccount").toString()));
        String msg=null;

        if(user.getUseraccount().equals(params.get("userAccount").toString())){

            user.setUserphone(params.get("userPhone").toString());
            user.setUsersex(params.get("userSex").toString());
            user.setUsersign(params.get("userSign").toString());
            user.setUserpassword(params.get("userPassword").toString());
            user.setUsername(params.get("userName").toString());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                user.setUserbirth(simpleDateFormat.parse(params.get("userBirth").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setUseraddress(params.get("userAddress").toString());
            userService.uploadUser(user);
            session.setAttribute("user",user);
            System.out.println("user:"+user);
            modelAndView.addObject("msg","修改成功");
             msg="修改成功";
        }else{
             msg="修改失败";
        }



        return ResultData.success(msg,"");
    }

    @RequestMapping("/managerUpdateUserInformationPage")
    public
    ModelAndView managerUpdateUserInformationPage(HttpSession session, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/manager/userMessage");
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        modelAndView.addObject("u",userService.findOne(Integer.parseInt(params.get("id").toString())));
        return modelAndView;
    }

    @RequestMapping("/managerUpdateUserInformation")
    public @ResponseBody
    Data managerUpdateUserInformation(HttpSession session, HttpServletRequest request){
        User manager = (User) session.getAttribute("user");
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        String msg=null;
        User user  = new User();

        if(manager.getUsertype()==0){
            user.setUserid(Integer.parseInt(params.get("id").toString()));
            user.setUserphone(params.get("userPhone").toString());
            user.setUsersex(params.get("userSex").toString());
            user.setUsersign(params.get("userSign").toString());
            user.setUserpassword(params.get("userPassword").toString());
            user.setUsername(params.get("userName").toString());
            user.setUsertype(Integer.parseInt(params.get("userType").toString()));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                user.setUserbirth(simpleDateFormat.parse(params.get("userBirth").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setUseraddress(params.get("userAddress").toString());
            userService.managerUploadUser(user);


            msg="修改成功";
        }else {
            msg="修改失败";
        }



        return ResultData.success(msg,"");
    }


    @RequestMapping("/allUsers")
    public @ResponseBody Data allUsers(HttpSession session,HttpServletRequest request){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        User user = (User) session.getAttribute("user");

        /*System.out.println(">>>>>>>>>>>>>>>>>>>>"+JSON.toJSONString(params));*/
        int pageNo = Integer.parseInt(params.get("page").toString()) ;
        int pageSize =Integer.parseInt(params.get("limit").toString());
        PageHelper.startPage(pageNo, pageSize);
        if (user.getUsertype()==0) {
            List<User> lists=  userService.findAll(params);
            PageInfo<User> pageInfo = new PageInfo<>(lists);

            return ResultData.success("成功", (int) pageInfo.getTotal(),pageInfo.getList());
        }


        return ResultData.success("","");
    }

    @RequestMapping("/deleteUser")
    public @ResponseBody Data deleteUser(HttpSession session,HttpServletRequest request){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        User user = (User) session.getAttribute("user");

        if (user.getUsertype()==0) {
            int i = userService.deleteUserById(Integer.parseInt(params.get("id").toString()));

            return ResultData.success("成功", i);
        }


        return ResultData.success("","");
    }


}
